package edu.unc.comp410.colemangeer.model;

import edu.unc.comp410.colemangeer.data.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.omg.CORBA.portable.ApplicationException;

import edu.unc.comp410.colemangeer.data.IProductGateway;

/**
/*	Additional items in the Catalog:
		--- CatalogId = 26, name = KyeBooks, products 20-24, price 1000.00
		--- CatalogId = 27,name = AllRings, products 10-12, price  110,000.00
		--- CatalogId = 28,name = CompleteMiddleEarth, products 13 + AllRings +
		            25, price  110,200.00
		--- CatalogId = 29,name = MagicalWeapons, products 18 and 25,
				price 400.00
 * 
 * @author ccoleman
 *
 */
public class CatalogBuilder {
	
	private DataProvider dataProvider;
	private IProductGateway productData;
	
	public CatalogBuilder(IProductGateway dataObj) throws ApplicationException{
		productData = dataObj;
	}
	
	public Catalog getCatalog(){
		Catalog catalog = new Catalog();
		Set<CatalogItem> catalogItems = new HashSet<CatalogItem>();
		catalogItems.addAll(getAllSingletons());
		catalogItems.addAll(getAllBundles()); 
		
		//fill cache
		//no, the cache is INVISIBLE. It is ONBOARD ProductGateway.
		//we NEVER call it or talk to it.
		
		//eh, GUI is using it...sorry
		DB_Cache2.setTableSize( productData.getTableSize() ); //set the size
	    DB_Cache2 dbc = new DB_Cache2( catalogItems ); //fill the cache
		 
		
		for (CatalogItem item: catalogItems){
			catalog.addCatalogItem(item);
		}
		return catalog;
	}
	
	private Set<CatalogItem> getAllSingletons(){

		Set<Product> allProducts = productData.getAllProducts();
		Set<CatalogItem> singletonItems = new HashSet<CatalogItem>();
		for (Product p: allProducts){
			
			List<ISellable> detailList = new ArrayList<ISellable>();
			detailList.add(p);
			
			CatalogItem item = 
				new CatalogItem(p.getId(),p.getName(),p.getPrice(), detailList, p.getProductCategories());
			singletonItems.add(item);
		}
		return singletonItems;		
	}
	

	private Set<CatalogItem> getAllBundles(){
		
		Set<CatalogItem> bundleItems = new HashSet<CatalogItem>();
		
		bundleItems.add(getKyeBooks());
		bundleItems.add(getAllRings());
		bundleItems.add(getMiddleEarth());
		bundleItems.add(getAllWeapons());
		
		return bundleItems;
	}
	
	//--- CatalogId = 26, name = KyeBooks, products 20-24, price 1000.00
	private CatalogItem getKyeBooks(){
		List<ISellable> books = new ArrayList<ISellable>();
		Set<ProductCategory> categories = new HashSet<ProductCategory>();
		
		for (int i = 20; i < 25; i++){
			Product book = productData.getByID(i);
			books.add(book);
			categories.addAll(book.getProductCategories());
		}
		return new CatalogItem(26,"KyeBooks",1000.00,books,categories);
	}

	//--- CatalogId = 27,name = AllRings, products 10-12, price  110,000.00
	private CatalogItem getAllRings(){
		List<ISellable> rings = new ArrayList<ISellable>();
		Set<ProductCategory> categories = new HashSet<ProductCategory>();
		
		for (int i = 10; i < 13; i++){
			Product ring = productData.getByID(i);
			rings.add(ring);
			categories.addAll(ring.getProductCategories());
		}
		return new CatalogItem(27,"AllRings",110000.00,rings,categories);
	}
	
	//--- CatalogId = 28,name = CompleteMiddleEarth, products 13 + AllRings +
    // 25, price  110,200.00
	
	private CatalogItem getMiddleEarth(){
		List<ISellable> items = new ArrayList<ISellable>();
		Set<ProductCategory> categories = new HashSet<ProductCategory>();
		Product p;
		
		p = productData.getByID(13);
		items.add(p);
		categories.addAll(p.getProductCategories());
		
		CatalogItem allRings = getAllRings();
		items.add(allRings);
		//categories.addAll(allRings.)) TODO: implement CatalogItem.getAllCategories
		
		p = productData.getByID(25);
		items.add(p);
		categories.addAll(p.getProductCategories());

		return new CatalogItem(28,"AllRings",110200.00,items,categories);
	}
	
//	--- CatalogId = 29,name = MagicalWeapons, products 18 and 25,
//	price 400.00*/
	private CatalogItem getAllWeapons(){
		List<ISellable> items = new ArrayList<ISellable>();
		Set<ProductCategory> categories = new HashSet<ProductCategory>();
		Product p;
		
		p = productData.getByID(18);
		items.add(p);
		categories.addAll(p.getProductCategories());
		
		p = productData.getByID(25);
		items.add(p);
		categories.addAll(p.getProductCategories());

		return new CatalogItem(29,"MagicalWeapons",400.00,items,categories);
	}
	
}
